package com.panda.list;

import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ListView;

import com.panda.list.ServiceTag.ServiceTagAdapter;
import com.panda.list.ServiceTag.ServiceTagListView;
import com.panda.list.allapps.AllAppsAdapter;
import com.panda.list.allapps.AllAppsGridView;
import com.panda.list.allapps.AllAppsLayout;
import com.panda.list.info.ApplicationInfo;
import com.panda.list.info.ApplicationManager;
import com.panda.list.views.HorizontalListView;

public class PandaListTestActivity extends Activity {

	ListView list1;
	ListView list2;


	Button btnNoti;
	Button mBtn_Tv;
	Button mBtn_Vod;
	Button mBtn_Edu;
	Button btnAllApps;
	Button mBtn_Applist;

	ApplicationManager am;

	ServiceTagListView mListView_Tv;
	ServiceTagListView mListView_Edu;
	ServiceTagListView mListView_Vod;

	ServiceTagAdapter mAdapter_Tv;
	ServiceTagAdapter mAdapter_Edu;
	ServiceTagAdapter mAdapter_Vod;

	AllAppsLayout allAppsLayout;
	AllAppsGridView allAppsView;
	AllAppsAdapter allAppsAdapter;
	
	
	View mView_AppList;
	HorizontalListView mHorizontalListView_FirApp;
	HorizontalListView mHorizontalListView_SecApp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		dataInit();
		layoutInit();
		eventInit();
	}

	private void dataInit() {

		ApplicationInfo info;
		am = new ApplicationManager();

		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		PackageManager pm = getPackageManager();

		List<ResolveInfo> apps = getPackageManager().queryIntentActivities(mainIntent, 0);

		Random random = new Random();

		for (ResolveInfo rInfo : apps) {
			info = new ApplicationInfo(this, pm, rInfo);
			info.setTag(random.nextInt(7));
			am.addApp(info);
		}
	}

	private void layoutInit() {

		this.mListView_Tv = (ServiceTagListView) findViewById(R.id.include_tvTag).findViewById(R.id.listview_service);
		this.mListView_Edu = (ServiceTagListView) findViewById(R.id.include_eduTag).findViewById(R.id.listview_service);
		this.mListView_Vod = (ServiceTagListView) findViewById(R.id.include_vodTag).findViewById(R.id.listview_service);

		this.mAdapter_Tv = new ServiceTagAdapter(this, ApplicationInfo.TAG_TV);
		this.mListView_Tv.setAdapter(this.mAdapter_Tv);
		this.mListView_Tv.setTag(ApplicationInfo.TAG_TV);
		
		this.mAdapter_Edu = new ServiceTagAdapter(this, ApplicationInfo.TAG_EDU);
		this.mListView_Edu.setAdapter(this.mAdapter_Edu);
		this.mListView_Edu.setTag(ApplicationInfo.TAG_EDU);
		this.mListView_Edu.setVisibility(View.INVISIBLE);
		
		this.mAdapter_Vod = new ServiceTagAdapter(this, ApplicationInfo.TAG_VOD);
		this.mListView_Vod.setAdapter(this.mAdapter_Vod);
		this.mListView_Vod.setTag(ApplicationInfo.TAG_VOD);
		this.mListView_Vod.setVisibility(View.INVISIBLE);

		allAppsLayout = (AllAppsLayout) findViewById(R.id.layout_allapps);
		allAppsLayout.setVisibility(View.INVISIBLE);

		allAppsView = (AllAppsGridView) findViewById(R.id.allapps_gridview);
		allAppsAdapter = new AllAppsAdapter(this);
		allAppsView.setAdapter(allAppsAdapter);
		
		this.mView_AppList = findViewById(R.id.include_applist);
		this.mView_AppList.setTag(ApplicationInfo.TAG_APP);
		this.mHorizontalListView_FirApp = (HorizontalListView) this.mView_AppList.findViewById(R.id.listview_first);
		/*this.mHorizontalListView_SecApp = (HorizontalListView) this.mView_AppList.findViewById(R.id.listview_seconds);*/
		
		this.mHorizontalListView_FirApp.setAdapter(this.allAppsAdapter);
		//this.mHorizontalListView_SecApp.setAdapter(this.allAppsAdapter);

		this.mHorizontalListView_FirApp.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Log.i("CH_DEBUG", "onItemClick");
				
			}
			
		});
		
		this.mHorizontalListView_FirApp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				Log.i("CH_DEBUG", "onItemSelected()");
			
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				Log.i("CH_DEBUG", "onNothingSelected");
			}
		});
		
		this.mView_AppList.setVisibility(View.INVISIBLE);
	}

	private void eventInit() {

		btnNoti = (Button) findViewById(R.id.btn_noti);
		btnNoti.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				for (int i=0; i<5; i++) {
					am.removeApp(am.getAppList().get(i));
				} 
				am.onNotifyDataChanged();
			}
		});

		mBtn_Tv = (Button) findViewById(R.id.btn_tv);
		mBtn_Tv.setTag(ApplicationInfo.TAG_TV);
		mBtn_Edu = (Button) findViewById(R.id.btn_edu);
		mBtn_Edu.setTag(ApplicationInfo.TAG_EDU);
		mBtn_Vod = (Button) findViewById(R.id.btn_vod);
		mBtn_Vod.setTag(ApplicationInfo.TAG_VOD);
		
		mBtn_Applist = (Button) findViewById(R.id.btn_applist);
		mBtn_Applist .setTag(ApplicationInfo.TAG_APP);

		OnClickListener onClickListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Object object = v.getTag();
				
				if ( v.getId() == R.id.btn_applist)
					mView_AppList.requestFocus();
				
				if ( object instanceof Integer ) {
					mListView_Tv.setVisibility(object==mListView_Tv.getTag() ? View.VISIBLE : View.INVISIBLE);
					mListView_Edu.setVisibility(object==mListView_Edu.getTag() ? View.VISIBLE : View.INVISIBLE);
					mListView_Vod.setVisibility(object==mListView_Vod.getTag() ? View.VISIBLE : View.INVISIBLE);
					mView_AppList.setVisibility(object==mView_AppList.getTag() ? View.VISIBLE : View.INVISIBLE);
				}

				allAppsLayout.setVisibility(View.INVISIBLE);
			}
		};

		mBtn_Tv.setOnClickListener(onClickListener);
		mBtn_Edu.setOnClickListener(onClickListener);
		mBtn_Vod.setOnClickListener(onClickListener);
		mBtn_Applist.setOnClickListener(onClickListener);

		btnAllApps = (Button) findViewById(R.id.btn_allapps);
		btnAllApps.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				mListView_Tv.setVisibility(View.INVISIBLE);
				mListView_Edu.setVisibility(View.INVISIBLE);
				mListView_Vod.setVisibility(View.INVISIBLE);
				mView_AppList.setVisibility(View.INVISIBLE);
				allAppsLayout.setVisibility(View.VISIBLE);
			}
		});
	}

	@Override
	protected void onDestroy() {
		am.allClear();
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}